package org.shuma.goods.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.shuma.domain.pojo.Result;
import org.shuma.goods.domain.DTO.ProductDTO;
import org.shuma.goods.domain.entity.ProductBasicInfo;
import org.shuma.goods.domain.entity.ProductDetailsInfo;
import org.shuma.goods.mapper.ProductBasicInfoDao;
import org.shuma.goods.service.ProductBasicInfoServiceByXingWang;
import org.shuma.goods.service.ProductDetailsInfoService;
import org.shuma.productPriceType.facade.ProductPriceTypeFacade;
import org.shuma.productPriceType.model.dto.ProductPriceTypeDTO;
import org.shuma.productPriceType.model.dto.UpdatePriceTypeDTO;
import org.shuma.proxy.facade.Proxy;
import org.shuma.proxy.facade.entity.ProductProxyDTO;
import org.shuma.supplier.domain.dto.SupplierDTO;
import org.shuma.supplier.domain.dto.UpdateSupplierDTO;
import org.shuma.supplier.domain.vo.SupplierVO;
import org.shuma.supplier.facade.SupplierFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (ProductBasicInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-06-18 13:50:00
 */
@Service("ProductBasicInfoServiceByXingWangImpl")
public class ProductBasicInfoServiceByXingWangImpl implements ProductBasicInfoServiceByXingWang{
    @SofaReference(interfaceType = Proxy.class,uniqueId = "${service.proxy.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private Proxy proxy;
    @SofaReference(interfaceType = SupplierFacade.class, uniqueId = "${service.supplier.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private SupplierFacade supplierFacade;
    @SofaReference(interfaceType = ProductPriceTypeFacade.class, uniqueId = "${service.productPriceType.unique.id}",
           binding = @SofaReferenceBinding(bindingType = "bolt"))
    private ProductPriceTypeFacade productPriceTypeFacade;

    @Resource
    private ProductBasicInfoDao productBasicInfomapper;

    @Autowired
    private ProductDetailsInfoService productDetailsInfoService;
    /**
     * 通过 productId查询单条数据
     * @param productId 主键
     * @return 实例对象
     */
    @Override
    public ProductBasicInfo queryById(Long productId) {
        return this.productBasicInfomapper.queryById(productId);
    }

    /**
     * 分页查询
     * @param productBasicInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ProductBasicInfo> queryByPage(ProductBasicInfo productBasicInfo, PageRequest pageRequest) {
        long total = this.productBasicInfomapper.count(productBasicInfo);
        return new PageImpl<>(this.productBasicInfomapper.queryAllByLimit(productBasicInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     * @return 实例对象
     */
    @Override
    public ProductBasicInfo insert(ProductDTO productDTO, MultipartFile file) {
        ProductBasicInfo productBasicInfo = new ProductBasicInfo();
        //将供应商信息传给微服务
        SupplierDTO supplierDTO = new SupplierDTO();
        supplierDTO.setSupplierName(productDTO.getSupplierName());
        supplierDTO.setSupplierPhone(productDTO.getSupplierPhone());
        //todo 应该将供应商存到表中的主键id返回
        Result result = supplierFacade.addSupplier(supplierDTO);
        Long supplier = (Long)result.getData();
        BeanUtils.copyProperties(productDTO,productBasicInfo);
        productBasicInfo.setProductSupplier(supplier);
        //先添加商品详情表
        ProductDetailsInfo productDetailsInfo = productDetailsInfoService.insert(productDTO, file);
        productBasicInfo.setProductCheckState(6);
        productBasicInfo.setPDetailsId(productDetailsInfo.getPDetailsId());
        //添加商品基本信息表
        this.productBasicInfomapper.insert(productBasicInfo);
        //将商品价格信息传递给微服务
        Long productId = productBasicInfo.getProductId();
        int money = 0, integral = 0, moneyIntegral = 0;
        if (productDTO.getProductTypeId() == 7) {money = 1;integral = 1;moneyIntegral = 1;}
        if (productDTO.getProductTypeId() == 6) {money = 1;integral = 1;
            moneyIntegral = 0;}
        if (productDTO.getProductTypeId() == 5) {money = 1;moneyIntegral = 1;
            integral = 0;}
        if (productDTO.getProductTypeId() == 4) {money = 0;
            integral = 1;moneyIntegral = 1;}
        if (productDTO.getProductTypeId() == 3) {money = 1;
            integral = 0;moneyIntegral = 0;}
        if (productDTO.getProductTypeId() == 2) {money = 0 ; integral = 0;moneyIntegral = 1;}
        if (productDTO.getProductTypeId() == 1) {money = 0;
            integral = 1;
            moneyIntegral = 0;}
        if(money == 0 && productDTO.getMoney() != null && !productDTO.getMoney().equals(0)) {
            throw new RuntimeException();
        };
        if(integral == 0 && productDTO.getIntegral() != null && !productDTO.getMoney().equals(0)){
            throw new RuntimeException();
        };
        if(moneyIntegral == 0 && productDTO.getMoneyIntegral() != null && !productDTO.getMoneyIntegral()
                .get(0).equals(0) && !productDTO.getMoneyIntegral().get(1).equals(0)){
            throw new RuntimeException();
        }
        int typeId = productPriceTypeFacade.getppt_Type(productId.intValue());
        if (typeId != 0){
            throw new RuntimeException();
        }
        ProductPriceTypeDTO productPriceTypeDTO = new ProductPriceTypeDTO();
        productPriceTypeDTO.setProductId(Math.toIntExact(productId));
        productPriceTypeDTO.setPptMoney(productDTO.getMoney());
        productPriceTypeDTO.setPptIntegral(productDTO.getIntegral());
        productPriceTypeDTO.setPptMoneyIntegral(productDTO.getMoneyIntegral());
        productPriceTypeFacade.insertppt_Type(productPriceTypeDTO);
        //远程调用proxy微服务
        Long proxyId = productBasicInfo.getProxyId();
        ProductProxyDTO productProxyDTO = new ProductProxyDTO();
        productProxyDTO.setProxyId(proxyId);
        productProxyDTO.setProductId(productId);
        proxy.addProxy(productProxyDTO);
        return productBasicInfo;
    }

    /**
     * 修改数据

     * @return 实例对象
     */
    @Override
    public ProductBasicInfo update(ProductDTO productDTO) {
        Long productSupplier = productDTO.getProductSupplier();
        String supplierName = productDTO.getSupplierName();
        String supplierPhone = productDTO.getSupplierPhone();
//判断供应商id是否存在并且供应商名字或者供应商电话有一个存在
        if (productSupplier != null && (supplierPhone != null || supplierName != null)){
            Result result = supplierFacade.getSupplier(Math.toIntExact(productSupplier));
            SupplierVO data =(SupplierVO) result.getData();
            if (data!=null){
                SupplierDTO supplierDTO = new SupplierDTO();
                UpdateSupplierDTO updateSupplierDTO = new UpdateSupplierDTO();
                updateSupplierDTO.setSupplierId(data.getSupplierId());
                supplierDTO.setSupplierName(supplierName);
                supplierDTO.setSupplierPhone(supplierPhone);
                supplierFacade.addSupplier(supplierDTO);
            }
        }
        ProductBasicInfo productBasicInfo = new ProductBasicInfo();
        BeanUtils.copyProperties(productDTO,productBasicInfo);

        long productId = productBasicInfo.getProductId();
        int typeId = productPriceTypeFacade.getppt_Type((int) productId);
        int productTypeId = Math.toIntExact(productDTO.getProductTypeId());
        if (typeId == 0 && typeId == productTypeId){
            UpdatePriceTypeDTO updatePriceTypeDTO = new UpdatePriceTypeDTO();
            updatePriceTypeDTO.setProductId(Math.toIntExact(productId));
            updatePriceTypeDTO.setPptMoney(productDTO.getMoney());
            updatePriceTypeDTO.setPptIntegral(productDTO.getIntegral());
            updatePriceTypeDTO.setPptMoneyIntegral(productDTO.getMoneyIntegral());
            productPriceTypeFacade.updateppt_Type(updatePriceTypeDTO);
        }

        this.productBasicInfomapper.update(productBasicInfo);
        return this.queryById(productBasicInfo.getProductId());
    }
    /**
     * 删除数据
     * @return 是否成功
     */
    @Override
    public boolean deleteById(List<Long> idList) {
        int count = 0;
        for (Long productId : idList) {
            ProductBasicInfo productBasicInfo = productBasicInfomapper.queryById(productId);
            productBasicInfo.setIsDelete(1);
            ProductDetailsInfo productDetailsInfo = new ProductDetailsInfo();
            productDetailsInfo.setPDetailsId(productBasicInfo.getPDetailsId());
            productDetailsInfo.setIsDelete((short) 1);
            productDetailsInfoService.update(productDetailsInfo);
            productBasicInfomapper.update(productBasicInfo);
            proxy.deleteProxy(productId);
            count++;
        }
        return count == idList.size();
    }

    //通过商品创建人id查询商品
    @Override
    public List<Long> queryByCreatorId(Long userId) {
        List<ProductBasicInfo> productBasicInfoList  =  productBasicInfomapper.queryByCreatorId(userId);
        List<Long> productIdList = new ArrayList<>();
        for (ProductBasicInfo productBasicInfo : productBasicInfoList) {
            productIdList.add(productBasicInfo.getProductId());
        }
        return productIdList;
    }
    //根据商品的id查询商品的创建人
    @Override
    public Long getCreatorId(Long productId) {
        ProductBasicInfo productBasicInfo = this.queryById(productId);
        Long creator = productBasicInfo.getCreator();
        return creator;
    }
}
